package com.info.factory;

import java.util.Locale;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.info.ClassInfo;
import com.info.factory.db.ClassFactoryDbHelper;
import com.info.factory.db.FeatFactoryDbHelper;

public class ClassFactory {
	
	private static ClassFactory INSTANCE;
	private ClassFactoryDbHelper helper;
	private String nameColumn[] = {TableColumn.NAME.name().toLowerCase(Locale.CANADA)};
	
	private enum TableColumn{
		ID,
		NAME,
		DESCRIPTION,
		BENEFIT,
		ATTACK_PROGRESS,
		FORTITUDE_PROGRESS,
		REFLEX_PROGRESS,
		WILL_PROGRESS,
		SKILL_PER_LEVEL,
		HIT_DIE,
		SKILL_LIST
	};
	
	public static ClassFactory getInstance(Context context){
		if (INSTANCE == null){
			INSTANCE = new ClassFactory(context);
		}
		return INSTANCE;
	}
	
	private ClassFactory(Context context){
		helper = new ClassFactoryDbHelper(context);
	}
	
	public ClassInfo getClassInfo(String name){
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor c = database.query(FeatFactoryDbHelper.TABLE_NAME, nameColumn, TableColumn.NAME.name().toLowerCase(Locale.CANADA) + " = " + name,null, null, null, null);
		if (c.getCount() != 1){
			c.close();
			return null;
		}
		return buildClass(c);
	}
	
	public ClassInfo buildClass(Cursor c){
		String description = c.getString(TableColumn.DESCRIPTION.ordinal());
		ClassInfo info = new ClassInfo();
		
		info.setName(c.getString(TableColumn.NAME.ordinal()));
		info.setDescription(description);
		
		info.setAttackProgress(c.getInt(TableColumn.ATTACK_PROGRESS.ordinal()));
		info.setFortProgress(c.getInt(TableColumn.FORTITUDE_PROGRESS.ordinal()));
		info.setRefProgress(c.getInt(TableColumn.REFLEX_PROGRESS.ordinal()));
		info.setWillProgress(c.getInt(TableColumn.WILL_PROGRESS.ordinal()));
		info.setSkillPerLevel(c.getInt(TableColumn.SKILL_PER_LEVEL.ordinal()));
		info.setHitDie(c.getInt(TableColumn.HIT_DIE.ordinal()));
		
		
		return info;
	}
}
